import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/Login',
    name: 'login',
    component: () => import('../components/Login.vue')
  },
  {
    path: '/',
    name: 'home',
    component: HomeView,
    // redirect: '/about',//页面默认加载的路由
    meta: {
      title: '首页',
      path: "/",
      component: () => import('../components/Console.vue'),
    },
    children: [
      {
        path: '/Console',
        component: () => import('../components/Console.vue'),
        meta: {
          title: '控制台',
          path: "/Console"
        }
      },
      {
        path: '/HouseInfo',
        component: () => import('../components/HouseInfoManage/HouseInfo.vue'),
        meta: {
          title: '房产管理',
          path: "/HouseInfo"
        }
      },
      {
        path: '/CarInfo', component: () => import('../components/OwnerInfoManage/CarInfo.vue'),
        meta: {
          title: '车辆管理',
          path: "/CarInfo"
        }
      },
      {
        path: '/OwnerInfo', component: () => import('../components/OwnerInfoManage/OwnerInfo.vue'),
        meta: {
          title: '人员管理',
          path: "/OwnerInfo"
        }
      },
      {
        path: '/ParkingPriceInfo', component: () => import('../components/ParkingManage/ParkingPriceInfo.vue'),
        meta: {
          title: '车位费用管理',
          path: "/ParkingPriceInfo"
        }
      },
      {
        path: '/ParkngSpaceInfo', component: () => import('../components/ParkingManage/ParkingSpaceInfo.vue'),
        meta: {
          title: '车位管理',
          path: "/ParkngSpaceInfo"
        }
      },
      {
        path: '/ParkingUseInfo', component: () => import('../components/ParkingManage/ParkingUseInfo.vue'),
        meta: {
          title: '车位使用管理',
          path: "/ParkingUseInfo"
        }
      },
      {
        path: '/ActiveInfo', component: () => import('../components/ServerManage/ActiveInfo.vue'),
        meta: {
          title: '活动管理',
          path: "/ActiveInfo"
        }
      },
      {
        path: '/ComplaintInfo', component: () => import('../components/ServerManage/ComplaintInfo.vue'),
        meta: {
          title: '投诉管理',
          path: "/ComplaintInfo"
        }
      },
      {
        path: '/RepairInfo', component: () => import('../components/ServerManage/RepairInfo.vue'),
        meta: {
          title: '报修管理',
          path: "/RepairInfo"
        }
      },
      {
        path: '/ChargesInfo', component: () => import('../components/ChargeManage/Chargesinfo.vue'),
        meta: {
          title: '收费项目',
          path: "/ChargesInfo"
        }
      },
      {
        path: '/ChargesDetailInfo', component: () => import('../components/ChargeManage/ChargesDetailInfo.vue'),
        meta: {
          title: '收费明细管理',
          path: "/ChargesDetailInfo"
        }
      },
      {
        path: '/RoleInfo', component: () => import('../components/SystemManage/RoleInfo.vue'),
        meta: {
          title: '角色管理',
          path: "/RoleInfo"
        }
      },
      {
        path: '/SystemInfo', component: () => import('../components/SystemManage/SystemInfo.vue'),
        meta: {
          title: '管理员列表',
          path: "/SystemInfo"
        }
      },
      {
        path: '/JurisdictionManage', component: () => import('../components/SystemManage/JurisdictionManage.vue'),
        meta: {
          title: '权限管理',
          path: "/JurisdictionManage"
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 导航守卫         
// 使用 router.beforeEach(进入路由之前) 注册一个全局前置守卫，判断用户是否登陆 
//next():用于控制路由跳转的行为
router.beforeEach((to, from, next) => {
  if (to.path === '/Login') {
    next();
  } else {
    let token = sessionStorage.getItem('token');
    if (token == null || token == '') {
      next('/Login');
    } else {
      next();
    }
  }
})


export default router
